﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace StockDetective
{
    public partial class Form1 : Form
    {
        Thread td1;
        Thread td2;
        Thread td3;
        Thread td4;
        Thread td5;
        Thread td6;
        Thread td7;
        Thread td8;
        delegate void algorithm();
        int done;
        bool errorListFormOn;

        public Form1()
        {
            InitializeComponent();
            done = 0;
            errorListFormOn = false;
            td1 = new Thread(new ParameterizedThreadStart(excute));
            td2 = new Thread(new ParameterizedThreadStart(excute));
            td3 = new Thread(new ParameterizedThreadStart(excute));
            td4 = new Thread(new ParameterizedThreadStart(excute));
            td5 = new Thread(new ParameterizedThreadStart(excute));
            td6 = new Thread(new ParameterizedThreadStart(excute));
            td7 = new Thread(new ParameterizedThreadStart(excute));
            td8 = new Thread(new ParameterizedThreadStart(excute));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            algorithm algorithm1 = new algorithm(Detective.crawl);
            algorithm algorithm2 = new algorithm(Detective.crawl);
            algorithm algorithm3 = new algorithm(Detective.crawl);
            algorithm algorithm4 = new algorithm(Detective.crawl);
            algorithm algorithm5 = new algorithm(Detective.crawl);
            algorithm algorithm6 = new algorithm(Detective.crawl);
            algorithm algorithm7 = new algorithm(Detective.crawl);
            algorithm algorithm8 = new algorithm(Detective.crawl);

            this.button1.Enabled = false;
            this.button2.Enabled = false;

            td1.Start(algorithm1);
            td2.Start(algorithm2);
            td3.Start(algorithm3);
            td4.Start(algorithm4);
            td5.Start(algorithm5);
            td6.Start(algorithm6);
            td7.Start(algorithm7);
            td8.Start(algorithm8);
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form2 errorListForm = new Form2();
            errorListForm.Show(this);
            button1.Enabled = false;
            button2.Enabled = false;
            errorListFormOn = true;
        }

        private void excute(Object analyze)
        {
            ((algorithm)analyze)();
            this.Invoke((EventHandler)delegate
            {
                doneOrNot();
            });
        }

        private void doneOrNot()
        {
            done++;
            if(done == 8)
            {
                this.button1.Enabled = true;
                this.button2.Enabled = true;
                Console.WriteLine("Finished");
                td1 = new Thread(new ParameterizedThreadStart(excute));
                td2 = new Thread(new ParameterizedThreadStart(excute));
                td3 = new Thread(new ParameterizedThreadStart(excute));
                td4 = new Thread(new ParameterizedThreadStart(excute));
                td5 = new Thread(new ParameterizedThreadStart(excute));
                td6 = new Thread(new ParameterizedThreadStart(excute));
                td7 = new Thread(new ParameterizedThreadStart(excute));
                td8 = new Thread(new ParameterizedThreadStart(excute));
                done = 0;
            }
        }

        public void buttonEnble()
        {
            if(errorListFormOn)
            {
                this.button1.Enabled = true;
                this.button2.Enabled = true;
            }
        }

    }
}
